/*
 * @Description: 计数器hooks
 * @Author: ZhangHan
 * @Date: 2025-07-23 16:25:09
 * @LastEditTime: 2025-07-23 16:32:00
 * @LastEditors: ZhangHan
 */
import { useState } from "react";

function useCounter(initialValue: number) {
  const [counter, setCount] = useState(initialValue);

  const addNum = (num?: number) => {
    if (num) {
      setCount(counter + num);
    } else {
      setCount(counter + 1);
    }
  };
  const reduceNum = () => {
    if (counter === 0) return;
    setCount(counter - 1);
  };

  return { counter, addNum, reduceNum };
}

export default useCounter;
